package com.hqyj.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hqyj.springboot.entity.Users;
import com.hqyj.springboot.json.R;
import com.hqyj.springboot.service.UsersService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("users")
@RestController
public class UsersController {

    @Resource
    private UsersService usersService;

    //新增用户的接口
    //insert into users values (?,?,?,?,?,?)
    @PostMapping("/")
    public R saveUser(@RequestBody Users u) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (usersService.insertUser(u)) {
                return R.success("新增用户成功！");
            } else {
                return R.fail("新增用户失败！");
            }
            //return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常!");
        }
    }

    //查询单个用户
    //select *from user where uid=? and password=? and age=?
    @GetMapping("/{uid}")
    public R queryUserByUid(@PathVariable("uid") Integer uid) {
        try {
            Users u = usersService.queryUserByUid(uid);
            return R.success("查询用户资料成功！", u);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //查询所有用户
    @GetMapping("/")
    public R queryAllUsers() {
        try {
            List<Users> usersList = usersService.queryAllUsers();
            return R.success("查询用户列表成功！", usersList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @PutMapping("/")
    public R modifyUser(@RequestBody Users u) {
        try {
            System.out.println("要更新的用户资料是：");
            System.out.println(u);
            if (usersService.modifyUsers(u)) {
                return R.success("更新用户资料成功！");
            } else {
                return R.fail("更新用户资料失败！");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //@RequstParam
    //http://localhost:8080/cruddemo/users/?uid=2
    /*
    @DeleteMapping("/")
    public R removeUserByUid(@RequestParam("uid") Integer uid) {
        try {
            if (usersService.removeUser(uid)) {
                return R.success("删除用户资料成功！");
            } else {
                return R.fail("删除用户资料失败！");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }*/

    @DeleteMapping("/{uid}")
    public R removeUserByUid(@PathVariable("uid") Integer uid) {
        try {
            if (usersService.removeUser(uid)) {
                return R.success("删除用户资料成功！");
            } else {
                return R.fail("删除用户资料失败！");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/pager/{currentPageNumber}")
    public R queryUsersPager(@PathVariable Integer currentPageNumber) {
        try {
            IPage pager = usersService.queryUsersPager(currentPageNumber);
            return R.success("查询用户分页数据成功！", pager);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
